package com.sywl.glzh.domain;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findProductsByEnNameLike", "findProductsByNameLike", "findProductsByRemarkLike", "findProductsByEnRemarkLike", "findProductsByType" })
public class Product {

    private String name;

    private String enName;

    private String image;

    @Size(max = 50000)
    private String remark;

    @Size(max = 50000)
    private String enRemark;

    @ManyToOne
    @JoinColumn(name = "type_id")
    private ProductType type;

    private Integer sort;

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Product().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countProducts() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Product o", Long.class).getSingleResult();
    }

	public static List<Product> findAllProducts() {
        return entityManager().createQuery("SELECT o FROM Product o", Product.class).getResultList();
    }

	public static Product findProduct(Long id) {
        if (id == null) return null;
        return entityManager().find(Product.class, id);
    }

	public static List<Product> findProductEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Product o", Product.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Product attached = Product.findProduct(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Product merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Product merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getEnName() {
        return this.enName;
    }

	public void setEnName(String enName) {
        this.enName = enName;
    }

	public String getImage() {
        return this.image;
    }

	public void setImage(String image) {
        this.image = image;
    }

	public String getRemark() {
        return this.remark;
    }

	public void setRemark(String remark) {
        this.remark = remark;
    }

	public String getEnRemark() {
        return this.enRemark;
    }

	public void setEnRemark(String enRemark) {
        this.enRemark = enRemark;
    }

	public ProductType getType() {
        return this.type;
    }

	public void setType(ProductType type) {
        this.type = type;
    }

	public Integer getSort() {
        return this.sort;
    }

	public void setSort(Integer sort) {
        this.sort = sort;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public static TypedQuery<Product> findProductsByEnNameLike(String enName) {
        if (enName == null || enName.length() == 0) throw new IllegalArgumentException("The enName argument is required");
        enName = enName.replace('*', '%');
        if (enName.charAt(0) != '%') {
            enName = "%" + enName;
        }
        if (enName.charAt(enName.length() - 1) != '%') {
            enName = enName + "%";
        }
        EntityManager em = Product.entityManager();
        TypedQuery<Product> q = em.createQuery("SELECT o FROM Product AS o WHERE LOWER(o.enName) LIKE LOWER(:enName)", Product.class);
        q.setParameter("enName", enName);
        return q;
    }

	public static TypedQuery<Product> findProductsByEnRemarkLike(String enRemark) {
        if (enRemark == null || enRemark.length() == 0) throw new IllegalArgumentException("The enRemark argument is required");
        enRemark = enRemark.replace('*', '%');
        if (enRemark.charAt(0) != '%') {
            enRemark = "%" + enRemark;
        }
        if (enRemark.charAt(enRemark.length() - 1) != '%') {
            enRemark = enRemark + "%";
        }
        EntityManager em = Product.entityManager();
        TypedQuery<Product> q = em.createQuery("SELECT o FROM Product AS o WHERE LOWER(o.enRemark) LIKE LOWER(:enRemark)", Product.class);
        q.setParameter("enRemark", enRemark);
        return q;
    }

	public static TypedQuery<Product> findProductsByNameLike(String name) {
        if (name == null || name.length() == 0) throw new IllegalArgumentException("The name argument is required");
        name = name.replace('*', '%');
        if (name.charAt(0) != '%') {
            name = "%" + name;
        }
        if (name.charAt(name.length() - 1) != '%') {
            name = name + "%";
        }
        EntityManager em = Product.entityManager();
        TypedQuery<Product> q = em.createQuery("SELECT o FROM Product AS o WHERE LOWER(o.name) LIKE LOWER(:name)", Product.class);
        q.setParameter("name", name);
        return q;
    }

	public static TypedQuery<Product> findProductsByRemarkLike(String remark) {
        if (remark == null || remark.length() == 0) throw new IllegalArgumentException("The remark argument is required");
        remark = remark.replace('*', '%');
        if (remark.charAt(0) != '%') {
            remark = "%" + remark;
        }
        if (remark.charAt(remark.length() - 1) != '%') {
            remark = remark + "%";
        }
        EntityManager em = Product.entityManager();
        TypedQuery<Product> q = em.createQuery("SELECT o FROM Product AS o WHERE LOWER(o.remark) LIKE LOWER(:remark)", Product.class);
        q.setParameter("remark", remark);
        return q;
    }

	public static TypedQuery<Product> findProductsByType(ProductType type) {
        if (type == null) throw new IllegalArgumentException("The type argument is required");
        EntityManager em = Product.entityManager();
        TypedQuery<Product> q = em.createQuery("SELECT o FROM Product AS o WHERE o.type = :type order by sort asc", Product.class);
        q.setParameter("type", type);
        return q;
    }
}
